package net.zhengxinyun.performance.service.workSupervision;

import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.WorkScoreEntity;
import net.zhengxinyun.performance.filter.IndexFilter;
import net.zhengxinyun.performance.filter.WorkScoreFilter;
import net.zhengxinyun.performance.filter.WorkSupervisionFilter;
import net.zhengxinyun.performance.mapper.workSupervision.WorkScoreEntityMapper;
import net.zhengxinyun.performance.mapper.workSupervision.WorkSupervisionEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by hyp1202 on 2018/12/13.
 */
@Service
public class WorkScoreServiceImpl implements WorkScoreService {

    private WorkScoreEntityMapper workScoreEntityMapper;
    private WorkSupervisionEntityMapper workSupervisionEntityMapper;

    @Autowired
    public WorkScoreServiceImpl(WorkScoreEntityMapper workScoreEntityMapper, WorkSupervisionEntityMapper workSupervisionEntityMapper) {
        this.workScoreEntityMapper = workScoreEntityMapper;
        this.workSupervisionEntityMapper = workSupervisionEntityMapper;
    }

    @Override
    public List<WorkScoreFilter> query(WorkScoreFilter filter) {
        /*if ("4".equals(filter.getAudit())) {
            filter.setAudit(null);
        }
        //应前端要求，给我状态值5就是搜索全部
        if ("5".equals(filter.getAudit())) {
            filter.setAudit("");
        }*/
        List<WorkScoreFilter> list = workScoreEntityMapper.queryScore(filter);
        return list;
    }

    @Override
    public String delete(WorkScoreEntity workScoreEntity) {
        int i = workScoreEntityMapper.deleteByPrimaryKey(workScoreEntity.getId());
        if (i == 0){
            return "删除失败，请重试";
        }
        return "删除成功";
    }

    @Override
    public String insert(WorkScoreEntity workScoreEntity) {
        workScoreEntity.setAudit("0");
        int i = workScoreEntityMapper.insert(workScoreEntity);
        if (i == 0){
            return "添加失败，请重试";
        }
        return "添加成功";
    }


    @Override
    public String update(WorkScoreEntity entity) {
        entity.setAudit("0");
        int i = workScoreEntityMapper.updateByPrimaryKeySelective(entity);
        if (i == 0){
            return "修改失败。请重试";
        }
        return "修改成功";
    }

    @Override
    @Transactional
    public String adminPass(List<WorkScoreEntity> list) {
        for (WorkScoreEntity entity:list) {
            entity.setAudit("2");
            int i = workScoreEntityMapper.updateByPrimaryKeySelective(entity);
            if (i == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                String name = workSupervisionEntityMapper.selectByPrimaryKey(entity.getIndexId() ).getIndexName();
                return name+"得分审核通过失败";
            }
        }
        return "审核成功";
    }

    @Override
    public String responsibleAllPass(List<WorkScoreEntity> list) {
        for (WorkScoreEntity entity:list) {
            entity.setAudit("1");
            int i = workScoreEntityMapper.updateByPrimaryKeySelective(entity);
            if (i == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                String name = workSupervisionEntityMapper.selectByPrimaryKey(entity.getIndexId()  ).getIndexName();
                return name+"得分审核通过失败";
            }
        }
        return "审核成功";
    }

    @Override
    public Integer queryByTwoTableCount(WorkScoreFilter filter) {
        int i = workScoreEntityMapper.queryScoreCount(filter);
        return i;
    }

    //分数导入
    @Override
    public String importScore(MultipartFile file) {
        if (file.getOriginalFilename().endsWith(".xls") == false && file.getOriginalFilename().endsWith(".xlsx") == false) {
            return "文件后缀不对";
        }
            return "导入成功";

    }

    @Override
    public List<WorkScoreFilter> queryForAudit(WorkScoreFilter filter) {
        List<WorkScoreFilter> list = workScoreEntityMapper.queryByTwoTableForAudit(filter);
        return list;
    }

    @Override
    public Integer queryForAuditCount(WorkScoreFilter filter) {
        return workScoreEntityMapper.queryByTwoTableForAuditCount(filter);
    }

    @Override
    public String noPass(WorkScoreEntity entity) {
        entity.setAudit("3");
        int i = workScoreEntityMapper.updateByPrimaryKeySelective(entity);
        if (i == 0){
            return "回退失败，请重试";
        }
        return "回退成功";
    }

    //流程通过
    @Override
    public int auditProdure(WorkScoreEntity entity) {
        //entity.setAudit("1");
        if("1".equals(entity.getAudit())) {
            entity.setFinalScore(entity.getScore());
        }
        else {
            entity.setFinalScore(null);
        }
        return workScoreEntityMapper.auditProdure(entity);
    }

    @Override
    public List<WorkScoreFilter> indexWork(IndexFilter indexFilter) {
        WorkScoreFilter workScoreFilter = new WorkScoreFilter();
        workScoreFilter.setAudit("2");
        if (StringUtils.isBlank(indexFilter.getDateTime()) && StringUtils.isBlank(indexFilter.getMonthTime())){
            WorkScoreFilter filter = workScoreEntityMapper.queryForNewTime();
            if (filter == null){
                return null;
            }
            workScoreFilter.setDateTime(filter.getDateTime());
            workScoreFilter.setMonthTime(filter.getMonthTime());
            List<WorkScoreFilter> list = workScoreEntityMapper.queryScore(workScoreFilter);
            return list;
        }else {
            workScoreFilter.setDateTime(indexFilter.getDateTime());
            workScoreFilter.setMonthTime(indexFilter.getMonthTime());
            List<WorkScoreFilter> list = workScoreEntityMapper.queryScore(workScoreFilter);
            return list;
        }
    }

    @Override
    public Double queryForSum(IndexFilter indexFilter) {
        WorkScoreFilter workScoreFilter = workScoreEntityMapper.queryForNewTime();
        if (workScoreFilter == null || StringUtils.isBlank(workScoreFilter.getDateTime()) || StringUtils.isBlank(workScoreFilter.getMonthTime())){
            return 0.0;
        }
        indexFilter.setDateTime(workScoreFilter.getDateTime());
        indexFilter.setMonthTime(workScoreFilter.getMonthTime());
        Double sum = workScoreEntityMapper.queryForSum(indexFilter);
        return sum;
    }


}
